#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
调试spectral聚类问题的脚本
"""

import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

import pandas as pd
from src.analysis.clustering import ClusterAnalyzer
from src.core.config import Config

def test_spectral_clustering():
    """测试spectral聚类"""
    print("=== 开始测试spectral聚类 ===")
    
    # 创建测试数据
    data = {
        'age': [25, 30, 35, 40, 45, 50, 55, 60, 65, 70],
        'income': [30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000, 110000, 120000],
        'satisfaction': [3, 4, 5, 4, 3, 5, 4, 3, 4, 5]
    }
    df = pd.DataFrame(data)
    
    print(f"测试数据形状: {df.shape}")
    print(f"测试数据列: {list(df.columns)}")
    
    # 创建配置和分析器
    config = Config()
    analyzer = ClusterAnalyzer(config)
    
    print("\n=== 测试spectral算法 ===")
    try:
        result = analyzer.perform_clustering(
            df, 
            features=['age', 'income', 'satisfaction'], 
            algorithm='spectral', 
            n_clusters=3
        )
        
        print(f"结果类型: {type(result)}")
        print(f"结果键: {list(result.keys()) if isinstance(result, dict) else 'Not a dict'}")
        
        if result.get('error'):
            print(f"错误: {result['error']}")
        else:
            print(f"成功: {result.get('success', False)}")
            print(f"算法: {result.get('algorithm', 'Unknown')}")
            print(f"聚类数: {result.get('n_clusters', 'Unknown')}")
            
    except Exception as e:
        print(f"异常: {type(e).__name__}: {str(e)}")
        import traceback
        print(f"堆栈跟踪: {traceback.format_exc()}")

if __name__ == '__main__':
    test_spectral_clustering()